package com.leecode.section;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 给定 nums = [2, 7, 11, 15], target = 9
 * <p>
 * 因为 nums[0] + nums[1] = 2 + 7 = 9
 * 所以返回 [0, 1]
 * </p>
 *
 * @author qingshan
 * @since 2020-07-16 22:41
 */
public class TwoNumbersSum01 {

    public static void main(String[] args) {
        System.out.println(Arrays.toString(getIndexs(new int[]{2, 7, 11, 15}, 9)));
//        getIndexs(new int[]{2, 7, 11, 15}, 9);
    }

    public static int[] getIndexs(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int temp = target - nums[i];
            if (map.containsKey(temp)) {
                return new int[]{map.get(temp), i};
            }
            // map中存储的是value 和 key(index)
            map.put(nums[i], i);
        }
        return new int[]{-1, -1};
    }
}
